<?php

// ----------------------------------------------------------------------
// Source file:  $Source: /cvsroot/pn-commerce/pn-commerce/md5check.php,v $
//
// Last changes from:  $Author: landseer $
//
// Revision:           $Revision: 1.2 $
//
// Date of revision    $Date: 2004/08/25 05:59:12 $
//
// ----------------------------------------------------------------------

$version = '$Id: md5check.php,v 1.2 2004/08/25 05:59:12 landseer Exp $';
$cmd    = $_GET['cmd'];

switch($cmd)
{
    case 'status':
                if(file_exists("md5/lastupdate"))
                {
                    $last = file("md5/lastupdate");
                    echo $last[0];
                }
                break;
    case 'version':
                echo $version;
                break;
    case 'check':
                $filename = pnVarPrepForOS("md5/pncommerce/" . urldecode($_GET['filename']));
                $data = getmd5andrevision($filename);
                echo $data[1]. ":" . $data[0];
                break;
    case 'getlist':
                $basedir = "/var/www/update.pncommerce.de/md5/pncommerce/";
                $allfound = array();
                $allfound = dirscan($basedir);
                echo count($allfound) . "<br />";
                foreach( $allfound as $fname=>$fdata)
                {
                    echo $fname . ":" . $fdata['md5'] . ":" . $fdata['revision'] . ":" . $fdata['author'] . ":" . $fdata['date'] ."<br />";
                }
                /*
                echo "<pre>";
                print_r($allfound);
                echo "</pre>" . count($allfound) . " entries found";
                */
                break;
                
    default:
                echo "error";
}

function dirscan($dirname)
{
//    echo "<br />entering '$dirname'<br />";
    $found = array();
    $dir = opendir($dirname);
    while ( $file = readdir( $dir ) )
    {
        if( $file != "." && 
            $file != ".." && 
            $file != "index.html" && 
            $file != "pnimages" &&
            $file != "category_images" &&
            $file != "product_images"
            )
        {
            $file = $dirname . $file;
//            echo "--".$file."--";
            if(is_dir($file))
            {
//                echo "***";
                $newfound = array();
                $newfound = dirscan($file . "/" );
                $found = array_merge($found, $newfound);
            }
            else
            {
                $data = getmd5andrevision($file);
                $found[str_replace( "/var/www/update.pncommerce.de/md5/pncommerce/", "", $file)] = array( 'md5' => $data[0], 'revision' => $data[1], 'author' => $data[2], 'date' => $data[3]);
            }
//            echo "<br />";
        }
    }
    return $found;
}

function getmd5andrevision($filename)
{
    if(file_exists($filename))
    {
        $line = file($filename);
        $md5 = md5(implode("", $line));
        for( $x=0;$x<=20;$x++ )
        {
            // check for author
            if ( strstr( $line[$x], '$Author:' ) )
            {
                $t1 = substr_replace( $line[$x], "", 0, strpos( $line[$x], "$" )+1 );
                $t1 = substr_replace( $t1, "", strrpos( $t1, "$" ), strlen( $t1 ) );
                $t2 = explode( ":", $t1);
                $author = trim($t2[1]);
                continue;
            }
            // check for revision
            if ( strstr( $line[$x], '$Revision:' ) )
            {
                $t1 = substr_replace( $line[$x], "", 0, strpos( $line[$x], "$" )+1 );
                $t1 = substr_replace( $t1, "", strrpos( $t1, "$" ), strlen( $t1 ) );
                $t2 = explode( ":", $t1);
                $revision = trim($t2[1]);
                continue;
            }
            // check for date
            if ( strstr( $line[$x], '$Date:' ) )
            {
                $t1 = substr_replace( $line[$x], "", 0, strpos( $line[$x], "$" )+1 );
                $t1 = substr_replace( $t1, "", strrpos( $t1, "$" ), strlen( $t1 ) );
                $t2 = explode( " ", $t1);
                $date = trim($t2[1]);
                continue;
            }
        }
        return array( $md5, $revision, $author, $date);
    }
    return array( "", "", "", "");
}

/*
 * This is a copy of Postnukes pnVerPrepForOS() function
 */
function pnVarPrepForOS()
{
    static $search = array('!\.\./!si', // .. (directory traversal)
                           '!^.*://!si', // .*:// (start of URL)
                           '!^/!si',     // Forward slash (directory traversal)
                           '!^\\\\!si'); // Backslash (directory traversal)
    static $replace = array('',
                            '',
                            '_',
                            '_');

    $resarray = array();
    foreach (func_get_args() as $ourvar) {
        // Parse out bad things
        $ourvar = preg_replace($search, $replace, $ourvar);
        // Prepare var
        if (!get_magic_quotes_runtime()) {
            $ourvar = addslashes($ourvar);
        }
        // Add to array
        array_push($resarray, $ourvar);
    }
    // Return vars
    if (func_num_args() == 1) {
        return $resarray[0];
    } else {
        return $resarray;
    }
}
?>